﻿using System;
using System.Activities.Presentation.Model;
using System.Activities.Presentation.Services;
using System.Activities.Presentation.View;
using System.Windows;
using System.Windows.Media;

namespace Headsight.BPMN2WF.WF.Impl
{
    public class ModelItemFlowDecision : AbstractModelItem
    {
        public ModelItemFlowDecision(ModelItem modelItem, ViewStateService viewStateService, ViewService viewService)
            : base(modelItem, viewStateService, viewService)
        {
        }

        public override Size ShapeSize
        {
            get { return new Size(60,75); }
            set { throw new NotImplementedException(); }
        }

        public override Point AlignChildren()
        {
            // Alignment of Children not for Descision Shapes
            return new Point();
        }

        public override void ResetConnector()
        {
            // Set Connectors only if exactly two children exist
            if (ImmediateChildren.Count != 2) return;

            var child0 = ModelItemFactory.Create(ImmediateChildren[0], InnerViewStateService, InnerViewService);
            var child1 = ModelItemFactory.Create(ImmediateChildren[1], InnerViewStateService, InnerViewService);

            var childLeft = child0.ShapeLocation.X < child1.ShapeLocation.X ? child0 : child1;
            var childRight = childLeft == child0 ? child1 : child0;

            var leftStartX = ShapeLocation.X;
            var rightStartX = ShapeLocation.X + ShapeSize.Width;
            var leftRightStartY = ShapeLocation.Y + (ShapeSize.Height / 2);

            var leftEndX = childLeft.ShapeLocation.X + childLeft.ShapeSize.Width / 2;
            var leftEndY = childLeft.ShapeLocation.Y;

            var rightEndX = childRight.ShapeLocation.X + childRight.ShapeSize.Width / 2;
            var rightEndY = childRight.ShapeLocation.Y;

            var leftPointCollection = new PointCollection
                                      {
                                          new Point(leftStartX, leftRightStartY),
                                          new Point(leftEndX, leftRightStartY),
                                          new Point(leftEndX, leftEndY)
                                      };
            InnerViewStateService.StoreViewStateWithUndo(InnerItem, "TrueConnector", leftPointCollection);

            var rightPointCollection = new PointCollection
                                      {
                                          new Point(rightStartX, leftRightStartY),
                                          new Point(rightEndX, leftRightStartY),
                                          new Point(rightEndX, rightEndY)
                                      };
            InnerViewStateService.StoreViewStateWithUndo(InnerItem, "FalseConnector", rightPointCollection);
        }
    }
}
